package network.tcp.bio;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.net.Socket;

/**
 * @program: baseModule
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-27 04:02
 **/
public class TcpUploadClient {
    public static void main(String[] args) {
        client();
    }

    private static void client() {
        try (Socket socket = new Socket("localhost", 9000);
             BufferedOutputStream os = new BufferedOutputStream(socket.getOutputStream());
             BufferedInputStream fis = new BufferedInputStream(new FileInputStream("info.txt"));
             BufferedInputStream is = new BufferedInputStream(socket.getInputStream());

        ) {

            //
            byte[] bytes = new byte[1024];

            int len;
            while ((len = fis.read(bytes)) != -1) {

                os.write(bytes, 0, len);
            }
            os.flush();
            //这样流才能过去
            socket.shutdownOutput();

            //接收反馈
            if ((len = is.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, len));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }

    }
}
